//
//  ICEEMLive.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/9.
//  Copyright © 2020 zh. All rights reserved.
//

#import "ICEEMLive.h"
#import "ICEJavaScriptBridge.h"
#import "EMCrypt.h"
#if !TARGET_OS_SIMULATOR
#import <MGLivenessDetection/MGLivenessDetection.h>
#endif
@interface ICEEMLive ()

@end

@implementation ICEEMLive

+ (instancetype)sharedSingleton {
    static ICEEMLive *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [ICEEMLive sharedSingleton];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [ICEEMLive sharedSingleton];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [ICEEMLive sharedSingleton];
}

-(void)checkLive:(NSDictionary*)args
{
#if !TARGET_OS_SIMULATOR
    ICEWebInfo* vcinfo = [args objectForKey:@"vcinfo"];
    MGLiveManager *lm = [MGLiveManager share];
    [lm startFaceDetectionViewController:vcinfo.vc ClientID:@"4ea103e2-452f-48b3-8d41-a16b539f8603" ClienclientSecret:@"KnutuqZCb9WhqbbVbRFRmA7AiSkhIOkM" UIconfiguration:nil finish:^(UIImage *image, UIViewController *vc) {
        NSString* base64EncodedString = [image imageBase64StringWithMime];
        NSDictionary *dit = nil;
        if (base64EncodedString.length > 0) {
              dit = @{
                      @"status":@"success",
                      @"faceImage":base64EncodedString,
                      };
        } else {
            dit =  @{
                @"status":@"error_other",
                };
        }
        [ICEJavaScriptBridge sendReslutToJS:dit callInfo:args];
        UIViewController *currentvc = ICE_ROOT_NAVIGATION_CONTROLLER.viewControllers.lastObject;
        [currentvc dismissViewControllerAnimated:YES completion:nil];
    } error:^(MGLivenessDetectionFailedType errorType, UIViewController * _Nonnull vc) {
        [ICEJavaScriptBridge sendReslutToJS: @{@"status":@"error_other",
                                               @"errorCode":@(errorType)
        } callInfo:args];
        [vc dismissViewControllerAnimated:YES completion:nil];
    } cancel:^(UIViewController *vc) {
        [ICEJavaScriptBridge sendReslutToJS: @{@"status":@"error_cancel"} callInfo:args];
         [vc dismissViewControllerAnimated:YES completion:nil];
    }];
#endif
}

@end
